/*
* @Author: chenggh
* @Date: 2025-06-14
* @Link to Problem : https://oj.haizeix.com/problem/45
*/

// 尝试输出LCS
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int dp[1005][1005] = {0};
char x[1005], y[1005];
// 1: 左边转移过来
// 2: 上方转移过来
// 3: 左上方转移过来
int path[1005][1005] = {0};
int main(){
	cin >> x + 1 >> y + 1;
	int n = strlen(x + 1), m = strlen(y + 1);
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			if(dp[i - 1][j] > dp[i][j - 1]){
				dp[i][j] = dp[i - 1][j]; // 从上方转移过来
				path[i][j] = 1;
			} else { 
				dp[i][j] = dp[i][j - 1]; // 从左边转移过来(默认)
				path[i][j] = 2;
			}
			if(x[i] == y[j]) {
				path[i][j] = 3;
				dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
			}
		}
	}
	cout << dp[n][m] << endl;
	string show_LCS = "";
	int i = n, j = m;
	while (i > 0 && j > 0) {
		if(path[i][j] == 3){
			show_LCS.push_back(x[i]);
			i--, j--;
		}else if(path[i][j] == 2){
			j--;
		}else i--;
	}
	reverse(show_LCS.begin(), show_LCS.end());
	cout << show_LCS << endl;
	return 0;
}